package com.example.mylibrary.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.mylibrary.R;
import com.example.mylibrary.di.component.BaseComponent;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.OnKeyboardListener;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author mark.liu
 */
public abstract class BaseActivity<P extends IPresenter> extends RxAppCompatActivity {
    private final static String LAYOUT_LINEAR_LAYOUT = "LinearLayout";
    private final static String LAYOUT_FRAME_LAYOUT = "FrameLayout";
    private final static String LAYOUT_RELATIVE_LAYOUT = "RelativeLayout";
    private Unbinder mUnbinder;
    protected final String TAG = this.getClass().getSimpleName();
    @Inject
    protected P mPresenter;
    private boolean isControlKey = false;

    @Nullable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutRes());
        if (useEventBus()) {
            // 如果要使用eventbus请将此方法返回true
            EventBus.getDefault().register(this);
        }
        // 绑定到butterknife
        mUnbinder = ButterKnife.bind(this);
        // 依赖注入
//        setupActivityComponent(BaseApplication.getBaseComponent());
        initData();

    }

    /**
     * 提供AppComponent(提供所有的单例对象)给子类，进行Component依赖
     */
    protected abstract void setupActivityComponent(BaseComponent baseComponent);

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            // 释放资源
            mPresenter.onDestroy();
        }
        if (mUnbinder != Unbinder.EMPTY) {
            mUnbinder.unbind();
        }
        // 如果要使用eventbus请将此方法返回true
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        mPresenter = null;
        mUnbinder = null;
    }

    /**
     * 是否使用eventBus,默认为使用(false)，
     */
    protected boolean useEventBus() {
        return false;
    }

    protected boolean isControlKey() {
        return true;
    }

    /**
     * 加载xml布局文件
     */
    @LayoutRes
    protected abstract int getLayoutRes();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    public void launchActivity(@NonNull Intent intent) {
        ActivityUtils.startActivity(this, intent);
    }

    public void killMyself() {
        finish();
    }

    public Activity getActivity() {
        return this;
    }

    public void showToast(String msg) {
        ToastUtils.showLong(msg);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        ToastUtils.showShort("keyCode = " + keyCode);

//        if (keyCode == KeyEvent.KEYCODE_SETTINGS){
//            // 优化之前跳转设置界面全部黑屏的情况
//            Intent mIntent = new Intent();
//            mIntent.setClassName("com.android.tv.settings", "com.android.tv.settings.MainSettings");
//            startActivity(mIntent);
//        }
        if (isControlKey()) {
            // 控制按键跳转的应用
            String packName = "";
//            if (keyCode == 136) {
//                //电视
//                packName = "com.xiaojie.tv";
//            } else if (keyCode == 137) {
//                //回看
//                packName = "com.xiaojie.tv";
//            } else if (keyCode == 138) {
//                //点播
//                packName = "com.ktcp.video";
//            } else if (keyCode == 139) {
//                //应用
//                packName = "com.zjbb.superstore";
//            }else if (keyCode == 183) {
//                //电视
//                packName = "com.xiaojie.tv";
//            } else if (keyCode == 184) {
//                //回看
//                packName = "com.xiaojie.tv";
//            } else if (keyCode == 185) {
//                //点播
//                packName = "com.ktcp.video";
//            } else if (keyCode == 186) {
//                //应用
//                packName = "com.zjbb.superstore";
//            } else {
//                packName = "";
//            }
            if (keyCode == 138) {
                //点播
                packName = "com.ktcp.video";
            } else if (keyCode == 139) {
                //应用
                packName = "com.zjbb.superstore";
            }else if (keyCode == 183) {
                //电视
                packName = "com.xiaojie.tv";
            } else if (keyCode == 184) {
                //回看
                packName = "com.xiaojie.tv";
            } else if (keyCode == 185) {
                //点播
                packName = "com.ktcp.video";
            } else if (keyCode == 186) {
                //应用
                packName = "com.zjbb.superstore";
            } else if (keyCode == 131) {
                //回看
                packName = "com.xiaojie.tv";
            } else if (keyCode == 132) {
                //回看
                packName = "com.xiaojie.tv";
            } else if (keyCode == 133) {
                //回看
                packName = "com.ktcp.video";
            } else if (keyCode == 134) {
                //回看
                packName = "com.zjbb.superstore";
            } else if (keyCode == 136) {
                //回看
                packName = "com.xiaojie.tv";
            } else if (keyCode == 170) {
                //回看
                packName = "com.xiaojie.tv";
            } else {
                packName = "";
            }
            if (!TextUtils.isEmpty(packName)) {
                if (AppUtils.isAppInstalled(packName)) {
                    AppUtils.launchApp(packName);
                } else {
                    ToastUtils.showLong("未安装该应用,请先下载安装");
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}